package me.yangteng.zhihudaily.component;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;

import me.yangteng.zhihudaily.R;
import me.yangteng.zhihudaily.model.StoriesList;
import me.yangteng.zhihudaily.util.Helper;

/**
 * Created by MorenYang on 2017/12/18.
 */

public class NewsListAdapter extends BaseAdapter {
  private static final String TAG = "NewsListAdapter";
  private Context context;
  private List<StoriesList> storiesLists;

  public NewsListAdapter(Context context, List<StoriesList> storiesLists) {
    this.context = context;
    this.storiesLists = storiesLists;
  }

  @Override
  public int getCount() {
    return this.storiesLists.size();
  }

  @Override
  public Object getItem(int i) {
    return this.storiesLists.get(i);
  }

  @Override
  public long getItemId(int i) {
    return Integer.parseInt(this.storiesLists.get(i).getDate());
  }

  @Override
  public View getView(int i, View view, ViewGroup viewGroup) {
    ViewHolder viewHolder;
    if (view == null) {
      viewHolder = new ViewHolder();
      view = LayoutInflater.from(this.context).inflate(R.layout.news_list_layout, null);
      viewHolder.newsDate = view.findViewById(R.id.newsDate);
      viewHolder.newsList = view.findViewById(R.id.newsList);
      view.setTag(viewHolder);
    } else {
      viewHolder = (ViewHolder) view.getTag();
    }
    viewHolder.newsDate.setText(storiesLists.get(i).getDateTitle());
    viewHolder.newsList.setAdapter(new DailyNewsListAdapter(context, storiesLists.get(i).getStories()));
    Helper.setListViewHeightBasedOnChildren(viewHolder.newsList);
    return view;
  }

  private static class ViewHolder {
    TextView newsDate;
    ListView newsList;
  }

}
